package pub.cleangao.codegen.enquiry.sqlserver;

import org.apache.commons.lang3.StringUtils;
import pub.cleangao.codegen.GenConfigHolder;
import pub.cleangao.codegen.enquiry.ColumnEnquiry;
import pub.cleangao.codegen.enquiry.ColumnInfo;
import pub.cleangao.codegen.enquiry.JavaColumnInfo;
import pub.cleangao.codegen.util.ClassField;

import java.util.Map;
import java.util.Set;

public class SqlServerColumnEnquiry  extends ColumnEnquiry {

    private static final SqlServerTypeFormatter TYPE_FORMATTER = new SqlServerTypeFormatter();
    private static final String SHOW_SQL =
            """
            SELECT	 col.name AS column_name	, col.max_length AS MaxLength	, col.scale AS Scale	, col.is_nullable AS NULLABLE	, bt.name AS type	, col.is_identity	, ext.value AS comment	,(		SELECT COUNT(1) FROM sys.indexes IDX 		INNER JOIN sys.index_columns IDXC 		ON IDX.[object_id]=IDXC.[object_id] 		AND IDX.index_id=IDXC.index_id 		LEFT JOIN sys.key_constraints KC 		ON IDX.[object_id]=KC.[parent_object_id] 		AND IDX.index_id=KC.unique_index_id 		INNER JOIN sys.objects O 		ON O.[object_id]=IDX.[object_id] 		WHERE O.[object_id]=col.[object_id] 		AND O.type='U' 		AND O.is_ms_shipped=0 		AND IDX.is_primary_key=1 		AND IDXC.Column_id=col.column_id 	) AS is_pk FROM sys.columns col LEFT OUTER JOIN sys.types bt on bt.user_type_id = col.system_type_id LEFT JOIN sys.extended_properties ext ON ext.major_id = col.object_id AND ext.minor_id = col.column_id WHERE col.object_id = object_id('%s') ORDER BY col.column_id
            """;
    // SELECT col.name AS column_name , bt.name AS type , col.is_identity , ext.value as comment ,( SELECT COUNT(1) FROM sys.indexes IDX INNER JOIN sys.index_columns IDXC ON IDX.[object_id]=IDXC.[object_id] AND IDX.index_id=IDXC.index_id LEFT JOIN sys.key_constraints KC ON IDX.[object_id]=KC.[parent_object_id] AND IDX.index_id=KC.unique_index_id INNER JOIN sys.objects O ON O.[object_id]=IDX.[object_id] WHERE O.[object_id]=col.[object_id] AND O.type='U' AND O.is_ms_shipped=0 AND IDX.is_primary_key=1 AND IDXC.Column_id=col.column_id ) AS is_pk FROM sys.columns col LEFT OUTER JOIN sys.types bt on bt.user_type_id = col.system_type_id LEFT JOIN sys.extended_properties ext ON ext.major_id = col.object_id AND ext.minor_id = col.column_id WHERE col.object_id = object_id('front.bar') ORDER BY col.column_id;
    public SqlServerColumnEnquiry(GenConfigHolder config) {
        super(config);
    }

    @Override
    protected String getColumnInfoSql(String tableName) {
        return String.format(SHOW_SQL, tableName);
    }

    @Override
    protected ColumnInfo buildColumnInfo(Map<String, Object> rowMap) {
        Set<String> columnSet = rowMap.keySet();
        for (String columnInfo : columnSet) {
            rowMap.put(columnInfo.toUpperCase(), rowMap.get(columnInfo));
        }
        ColumnInfo columnInfo = new JavaColumnInfo();
        columnInfo.setColumnName(ClassField.convertString(rowMap.get("COLUMN_NAME")));
        columnInfo.setIsIdentity((Boolean)rowMap.get("IS_IDENTITY"));
        boolean isPk = (Integer)rowMap.get("IS_PK") == 1;
        columnInfo.setIsPk(isPk);
        String type = ClassField.convertString( rowMap.get("TYPE"));
        columnInfo.setType(TYPE_FORMATTER.format(type));
        columnInfo.setComment(ClassField.convertString(rowMap.get("COMMENT")));
        //sqlserver 字段长度
        String maxLength = ClassField.convertString(rowMap.get("MAXLENGTH"));
        columnInfo.setMaxLength(Integer.parseInt(StringUtils.isEmpty(maxLength) ? "0" : maxLength));
        //sqlserver 字段精度
        String scale = ClassField.convertString(rowMap.get("SCALE"));
        columnInfo.setScale(Integer.parseInt(StringUtils.isEmpty(scale) ? "0" : scale));
        String isNullable = ClassField.convertString(rowMap.get("NULLABLE"));
        columnInfo.setIsNullable("1".equalsIgnoreCase(isNullable));
        return columnInfo;
    }


}
